﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.JShop.UI
{
    public class UserPage : System.Web.UI.Page
    {
        public UserPage()
        {
            base.Load += new EventHandler(this.UserPage_Load);
        }
        /// <summary>
        /// 读取登录状态下Cookies中保存的会员名
        /// </summary>
        public string CookieString
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserName"];
                    if (Cookie != null)
                    {
                        return EncryptUtils.DecodeCookies(Cookie.Value);
                    }
                    else
                    {

                        return null;
                    }
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取会员ID
        /// </summary>
        public int CookieUserID
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserID"];
                    if (Cookie != null)
                    {
                        return Convert.ToInt32(EncryptUtils.DecodeCookies(Cookie.Value));
                    }
                    else
                    {

                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 获取会员等级
        /// </summary>
        public int CookieUserLevelID
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserLevelID"];
                    if (Cookie != null)
                    {
                        return Convert.ToInt32(EncryptUtils.DecodeCookies(Cookie.Value));
                    }
                    else
                    {

                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 判断是否登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserPage_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.AddHeader("cache-control", "");
            Response.CacheControl = "no-cache";

            HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserName"];
            HttpCookie Cookie_UserID = Request.Cookies["" + UIConfig.CookiesName + "UserID"];
            HttpCookie Cookie_UserLevelID = Request.Cookies["" + UIConfig.CookiesName + "UserLevelID"];
            if (Cookie == null || Cookie_UserID == null || Cookie_UserLevelID == null)
            {
                MessageUtils.Write("<script>alert('您还没有登录，请先登录！');top.location.href=\"/Member/Login.aspx?URL=" + Server.UrlEncode(Request.Url.PathAndQuery) + "\"</script>");
                Response.End();
            }
        }
        /// <summary>
        /// 会员退出
        /// </summary>
        protected void LoginOut()
        {
            HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserName"];
            HttpCookie Cookie_UserID = Request.Cookies["" + UIConfig.CookiesName + "UserID"];
            HttpCookie Cookie_UserLevelID = Request.Cookies["" + UIConfig.CookiesName + "UserLevelID"];
            if (Cookie != null && Cookie_UserID != null && Cookie_UserLevelID != null)
            {
                Cookie.Expires = DateTime.Now.AddDays(-1);
                Cookie.Value = null;
                Response.Cookies.Add(Cookie);

                Cookie_UserID.Expires = DateTime.Now.AddDays(-1);
                Cookie_UserID.Value = null;
                Response.Cookies.Add(Cookie_UserID);

                Cookie_UserLevelID.Expires = DateTime.Now.AddDays(-1);
                Cookie_UserLevelID.Value = null;
                Response.Cookies.Add(Cookie_UserLevelID);
            }
            Context.Session.Clear();
            Context.Session.Abandon();

            MessageUtils.Write("<script>top.location.href=\"/Member/Login.aspx\"</script>");
        }
    }
}
